/**
 * @Author ZhangCuirong
 * @Date 2025/9/4 11:18
 * @description:
 */
public class Solution724 {
    public int pivotIndex(int[] nums) {
        int[] prefixSum = new int[nums.length + 1];
        // 前缀和数组：prefixSum[i] 表示 nums[0..i-1] 的和（半开区间，避免边界判断）
        for (int i = 1; i <= nums.length; i++) {
            prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
        }
        for (int i = 0; i < nums.length ; i++) {
            //左侧和：nums[0..i-1] 的和 → 对应 prefixSum[i]（因为 prefixSum[i] = sum(0..i-1)）
            int left = prefixSum[i];
            // 右侧和：nums[i+1..n-1] 的和 → 总 sum - sum(0..i) = prefixSum[n] - prefixSum[i+1]
            int right = prefixSum[nums.length] - prefixSum[i + 1];//[i+1,num.length-1]
            if (left == right) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Solution724 solution = new Solution724();
        System.out.println(solution.pivotIndex(new int[]{1, 7, 3, 6, 5, 6}));
        System.out.println(solution.pivotIndex(new int[]{1, 2, 3}));
        System.out.println(solution.pivotIndex(new int[]{2, 1, -1}));
    }
}
